#!/bin/bash
# DATE: 2020-12-31 16:52:17
# FileName:     firewall_set.sh
# Description:  防火墙开关
# Depend:       NULL
#

##################################加载依赖##################################
Load_dependencies()
{
    local i depend var_tmp
    
    depend=(
        get_sys.sh
    )
    
    depend_url=https://gitee.com/uon/conf/raw/master/
    for i in "${depend[@]}";do
        var_tmp=$(echo $i|cut -c 1-$((${#i}-3)))
        if [[ ! $(type -t $var_tmp) == "function" ]];then
            
            if [ -f "$i" ];then
                if source "${i}";then
                    echo -e "\e[1;32mSuccessfully loaded ${i}\e[0m"
                else
                    echo -e "\e[1;31mFailed to load ${i}\e[0m"
                fi
            else
                if source <(curl -sLk "${depend_url}${i}");then
                    echo -e "\e[1;32mSuccessfully loaded ${i}\e[0m"
                else
                    echo -e "\e[1;31mFailed to load ${i}\e[0m"
                fi
            fi
        fi
    done
    
}
Load_dependencies
#############################################################################


function firewall_set()
{
    
    if [ "$1" == "off" ];then
        
        if [ "$(get_sys)" == "CentOS" ];then
            systemctl stop firewalld
            systemctl  disable firewalld
            systemctl status firewalld
            
            elif [ "$(get_sys)" == "Ubuntu" ];then
            ufw disable
            ufw status
            
        fi
        
        elif [ "$1" == "on" ];then
        
        if [ "$(get_sys)" == "CentOS" ];then
            systemctl start firewalld
            systemctl  enable firewalld
            systemctl status firewalld
            
            elif [ "$(get_sys)" == "Ubuntu" ];then
            echo -e "\e[1;31m此操作将中断ssh连接，是否继续？（y|n）\e[0m"
            ufw enable >/dev/null
            ufw status
            
        fi
    fi
}